#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
import pymongo
import traceback
import redis
import config
import time

#------------------------------------------------------------------------------ 
class Rlocal:
    @classmethod
    def __init__(self):
        try:
            self.re_lc = redis.Redis()
        except:
            traceback.print_exc()
#------------------------------------------------------------------------------ 
class Rserver:
    @classmethod
    def __init__(self):
        try:
            self.re_sv = redis.Redis(host=config.REDIS_SERVER, port=config.REDIS_PORT)
        except:
            traceback.print_exc()
#------------------------------------------------------------------------------ 
class Mongo_conn():
    def __init__(self):
        while True:
            try:
                self.dbmg = pymongo.MongoClient(config.MONGO_IP, config.MONGO_PORT).notification
                self.log = self.dbmg.log
                break
            except:
                time.sleep(1)
                traceback.print_exc()

#------------------------------------------------------------------------------ 
class MySQL_Conn():
    def __init__(self):
        while True:
            try:
    #             print "connect to Mysql"
                self.conn = MySQLdb.connect(host=config.MYSQL_HOST, port=config.MYSQL_PORT, user=config.MYSQL_USER, passwd=config.MYSQL_PASS, db=config.MYSQL_DB, charset='utf8')
                break;
            except:
                traceback.print_exc()
            time.sleep(1)
# #------------------------------------------------------------------------------ 
class cache(object,MySQL_Conn,Mongo_conn,Rlocal,Rserver):
    '''DB cache class'''
    _handlemysql = None
    _handmongodb = None
    _re_server = None
    _re_local = None
    def __init__(self):
#         self._handlemysql = MySQL_Conn()
#         if cache._handlemysql == None:
#             cache._handlemysql = Mysqldb()
        if cache._handmongodb == None:
            cache._handmongodb = Mongo_conn()
        if cache._re_local == None: 
            cache._re_local = Rlocal()
        if cache._re_server == None: 
            cache._re_server = Rserver()
             
#------------------------------------------------------------------------------ 
    def get_and_del(self, name, number):
        result = 0
        try:
#             redis.Redis.zremrangebyrank(name, 0, 1000)
            pipe = cache._re_local.re_lc.pipeline()
            pipe.multi()
            pipe.zrange(name, 0, number-1)
            pipe.zremrangebyrank(name, 0, number-1)
            res = pipe.execute()
            result = res[0]
        except:
            traceback.print_exc()
        return result
#------------------------------------------------------------------------------ 
# if __name__ == "__main__":
#     mongo = cache()._handmongodb.log
#     mongo.update({},{"$set": {"status": 3}})
    
#     a = cache()._handlemysql.mysql
#     results = 0
#     v_cursor = a.cursor()
#     v_cursor.callproc('get_next_task',())
#     results = v_cursor.fetchall()
#     v_cursor.close()
#     a.commit()
#     print results